package com.zjitc.chapter08.proxy;

import com.zjitc.chapter08.aspect.MyAspect;
import com.zjitc.chapter08.dao.UserDao;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyProxy implements InvocationHandler {
    private UserDao userDao;
    public Object createProxy(UserDao userDao) {
        this.userDao = userDao;
        ClassLoader classLoader = MyProxy.class.getClassLoader();
        Class[] interfaces = userDao.getClass().getInterfaces();
        return Proxy.newProxyInstance(classLoader, interfaces, this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        MyAspect myAspect = new MyAspect();
        myAspect.check_permission();
        Object obj = method.invoke(userDao, args);
        myAspect.log();
        return obj;
    }
}
